﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using EBSystem.Data;
using EBSystem.SQL;

namespace EBSystem.Pages.UserPages
{
    /// <summary>
    /// DeleteDeliveryAddressWindow.xaml 的交互逻辑
    /// </summary>
    public partial class DeleteDeliveryAddressWindow : Window
    {
        public DeleteDeliveryAddressWindow()
        {
            InitializeComponent();

            InitializeData();
        }

        private void deleteButton_Click(object sender, RoutedEventArgs e)
        {
            bool delete = false;
            for (int i = 0; i < deliveryAddress.Children.Count; i++)
            {
                if ((bool)((CheckBox)deliveryAddress.Children[i]).IsChecked)
                {
                    SQLConnection.DeleteDeliveryAddress(i);
                    delete = true;
                }
            }
            SQLConnection.LoadDeliveryAddress();
            if(delete)
                MessageBox.Show("删除成功");
            Close();
        }

        private void cancelButton_Click(object sender, RoutedEventArgs e)
        {
            Close();
        }

        public void InitializeData()
        {
            foreach (List<string> address in UserData.deliveryAddress)
            {
                TextBox textBox = new();
                textBox.Text = "";
                textBox.IsReadOnly = true;
                textBox.TextWrapping = TextWrapping.WrapWithOverflow;
                foreach (string item in address)
                {
                    textBox.Text += item;
                    textBox.Text += "  ";
                }

                CheckBox checkBox = new();
                checkBox.Content = textBox;
                checkBox.Margin = new(10);

                deliveryAddress.Children.Add(checkBox);
            }
        }
    }
}
